package com.app.couponsharelife.ui.main.me.order

import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.OrderClassBean
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.main.MainActivity
import com.app.couponsharelife.ui.main.home.shopweb.ShopWebActivity
import com.app.couponsharelife.ui.view.ScaleTransitionPagerTitleView
import com.app.couponsharelife.ui.viewfactory.NewOrderFragFactory
import com.app.couponsharelife.utils.AppUtils
import kotlinx.android.synthetic.main.activity_neworder.*
import kotlinx.android.synthetic.main.base_title_white.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 *Created by xct on 2019/1/28
 *Description:新的我的订单页面
 */
class NewOrderActivity:BaseActivity() {
    private var commonNavigator: CommonNavigator?=null
    private var newOrderFragFactory:NewOrderFragFactory?=null
    private var classList= arrayListOf<OrderClassBean>()
    var index=""
    private var source=""
    override val viewLayoutId: Int
        get() = R.layout.activity_neworder

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        index=intent.getStringExtra("Index")?:"0"
        source= intent!!.getStringExtra("source")?:"" //intent.putExtra("source", "ExperienceMenPay")
        basetitle_title.text="我的订单"
        basetitle_llRight.visibility=View.VISIBLE
        basetitle_tvRight.text="订单找回"
        order_tvZY.setOnClickListener {
            order_tvZY.setTextColor(resources.getColor(R.color.red_ff038))
            order_tvYX.setTextColor(resources.getColor(R.color.black))
            order_llYX.visibility=View.VISIBLE
            order_flZY.visibility=View.GONE
        }
        order_tvYX.setOnClickListener {
            order_tvZY.setTextColor(resources.getColor(R.color.black))
            order_tvYX.setTextColor(resources.getColor(R.color.red_ff038))
            order_flZY.visibility=View.VISIBLE
            order_llYX.visibility=View.GONE
        }
        basetitle_llclose.setOnClickListener {
            if ("ExperienceMenPay"==source){
                val intent=Intent(this, MainActivity::class.java)
                intent.putExtra("Type","ExperienceMen")
                intent.putExtra("Status","CEO")
                startActivity(intent)
                finish()
            }else {
                finish()
            }
        }
        basetitle_llRight.setOnClickListener {
            val url="http://www.yiyepay.cn/orderRecover/index.html?openTime=${System.currentTimeMillis()/1000}&sellerid=${AppUtils.getString(Contacts.USERID,"")}"
            val intent= Intent(this, ShopWebActivity::class.java)
            intent.putExtra("url", url)
            intent.putExtra("name", "找回订单")
            startActivity(intent)
        }
        setViewPager()
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        val fragment= SelfOrderFragment.newInstance(OrderClassBean("4","4","自营"))
        fragmentTransaction.replace(R.id.order_flZY,fragment).commit()
        if ("1"==index){
            //neworder_viewpager.setCurrentItem(1,true)
            order_tvZY.setTextColor(resources.getColor(R.color.black))
            order_tvYX.setTextColor(resources.getColor(R.color.red_ff038))
            order_flZY.visibility=View.VISIBLE
            order_llYX.visibility=View.GONE
        }
    }
    companion object {
        fun setA(a:String){
            
        }
    }
    private fun setList(){
        with(classList){
            add(OrderClassBean("0","0","全部"))
            //add(OrderClassBean("4","4","自营"))
            add(OrderClassBean("1","1","淘宝／天猫"))
            add(OrderClassBean("2","2","拼多多"))
            add(OrderClassBean("3","3","京东"))
        }
    }
    private fun setViewPager(){
        newOrderFragFactory= NewOrderFragFactory()
        setList()
        neworder_viewpager.offscreenPageLimit=5
        neworder_viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getItem(position: Int): Fragment {
                return newOrderFragFactory!!.createFragment(position,classList[position])
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return classList[position].name
            }
        }
        commonNavigator= CommonNavigator(this)
        commonNavigator!!.isAdjustMode=true
        //commonNavigator!!.isSmoothScroll=true
        commonNavigator!!.adapter=object : CommonNavigatorAdapter(){
            override fun getTitleView(p0: Context, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView= ScaleTransitionPagerTitleView(p0,1f)
                colorTransitionPagerTitleView.normalColor = resources.getColor(R.color.gray_66)
                colorTransitionPagerTitleView.textSize=14f
                colorTransitionPagerTitleView.paint.isFakeBoldText=true
                colorTransitionPagerTitleView.gravity=Gravity.CENTER
                colorTransitionPagerTitleView.selectedColor = resources.getColor(R.color.act_main_bot_nav_select_text_color)
                colorTransitionPagerTitleView.text = classList[p1].name
                colorTransitionPagerTitleView.setOnClickListener { neworder_viewpager.currentItem = p1 }
                return colorTransitionPagerTitleView
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getIndicator(p0: Context?): IPagerIndicator? {
                val indicator= LinePagerIndicator(p0)
                indicator.setColors(resources.getColor(R.color.act_main_bot_nav_select_text_color))
                indicator.mode= LinePagerIndicator.MODE_WRAP_CONTENT
                return indicator
            }
        }
        neworder_tablayout.navigator=commonNavigator
        /*val titleContainer=commonNavigator!!.titleContainer
        titleContainer.showDividers= LinearLayout.SHOW_DIVIDER_MIDDLE
        titleContainer.setPadding(20,0,0,0)
        titleContainer.dividerDrawable=object :ColorDrawable(){
            override fun getIntrinsicWidth(): Int {
                return 36
            }
        }*/
        ViewPagerHelper.bind(neworder_tablayout, neworder_viewpager)
    }

    override fun onBackPressed() {
        if ("ExperienceMenPay"==source){
            val intent=Intent(this, MainActivity::class.java)
            intent.putExtra("Type","ExperienceMen")
            intent.putExtra("Status","CEO")
            startActivity(intent)
            finish()
        }else {
            super.onBackPressed()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}